<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>functionmock_test.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="local_closure_goog_testing_functionmock_test.js.html">functionmock_test.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2008 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>goog.provide(&#39;goog.testing.FunctionMockTest&#39;);
<a name="line16"></a>goog.setTestOnly(&#39;goog.testing.FunctionMockTest&#39;);
<a name="line17"></a>
<a name="line18"></a>goog.require(&#39;goog.array&#39;);
<a name="line19"></a>goog.require(&#39;goog.string&#39;);
<a name="line20"></a>goog.require(&#39;goog.testing&#39;);
<a name="line21"></a>goog.require(&#39;goog.testing.FunctionMock&#39;);
<a name="line22"></a>goog.require(&#39;goog.testing.Mock&#39;);
<a name="line23"></a>goog.require(&#39;goog.testing.StrictMock&#39;);
<a name="line24"></a>goog.require(&#39;goog.testing.asserts&#39;);
<a name="line25"></a>goog.require(&#39;goog.testing.jsunit&#39;);
<a name="line26"></a>goog.require(&#39;goog.testing.mockmatchers&#39;);
<a name="line27"></a>
<a name="line28"></a>// Global scope so we can tear it down safely
<a name="line29"></a>var mockGlobal;
<a name="line30"></a>
<a name="line31"></a>function tearDown() {
<a name="line32"></a>  if (mockGlobal) {
<a name="line33"></a>    mockGlobal.$tearDown();
<a name="line34"></a>  }
<a name="line35"></a>}
<a name="line36"></a>
<a name="line37"></a>
<a name="line38"></a>//----- Tests for goog.testing.FunctionMock
<a name="line39"></a>
<a name="line40"></a>function testMockFunctionCallOrdering() {
<a name="line41"></a>  var doOneTest = function(mockFunction, success, expected_args, actual_args) {
<a name="line42"></a>    goog.array.forEach(expected_args, function(arg) { mockFunction(arg); });
<a name="line43"></a>    mockFunction.$replay();
<a name="line44"></a>    var callFunction = function() {
<a name="line45"></a>      goog.array.forEach(actual_args, function(arg) { mockFunction(arg); });
<a name="line46"></a>      mockFunction.$verify();
<a name="line47"></a>    };
<a name="line48"></a>    if (success) {
<a name="line49"></a>      callFunction();
<a name="line50"></a>    } else {
<a name="line51"></a>      assertThrows(callFunction);
<a name="line52"></a>    }
<a name="line53"></a>  };
<a name="line54"></a>
<a name="line55"></a>  var doTest = function(strict_ok, loose_ok, expected_args, actual_args) {
<a name="line56"></a>    doOneTest(goog.testing.createFunctionMock(), strict_ok,
<a name="line57"></a>              expected_args, actual_args);
<a name="line58"></a>    doOneTest(goog.testing.createFunctionMock(&#39;name&#39;), strict_ok,
<a name="line59"></a>              expected_args, actual_args);
<a name="line60"></a>    doOneTest(goog.testing.createFunctionMock(&#39;name&#39;, goog.testing.Mock.STRICT),
<a name="line61"></a>              strict_ok, expected_args, actual_args);
<a name="line62"></a>    doOneTest(goog.testing.createFunctionMock(&#39;name&#39;, goog.testing.Mock.LOOSE),
<a name="line63"></a>              loose_ok, expected_args, actual_args);
<a name="line64"></a>  };
<a name="line65"></a>
<a name="line66"></a>  doTest(true, true, [1, 2], [1, 2]);
<a name="line67"></a>  doTest(false, true, [1, 2], [2, 1]);
<a name="line68"></a>  doTest(false, false, [1, 2], [2, 2]);
<a name="line69"></a>  doTest(false, false, [1, 2], [1]);
<a name="line70"></a>  doTest(false, false, [1, 2], [1, 1]);
<a name="line71"></a>  doTest(false, false, [1, 2], [1]);
<a name="line72"></a>}
<a name="line73"></a>
<a name="line74"></a>function testMocksFunctionWithNoArgs() {
<a name="line75"></a>  var mockFoo = goog.testing.createFunctionMock();
<a name="line76"></a>  mockFoo();
<a name="line77"></a>  mockFoo.$replay();
<a name="line78"></a>  mockFoo();
<a name="line79"></a>  mockFoo.$verify();
<a name="line80"></a>}
<a name="line81"></a>
<a name="line82"></a>function testMocksFunctionWithOneArg() {
<a name="line83"></a>  var mockFoo = goog.testing.createFunctionMock();
<a name="line84"></a>  mockFoo(&#39;x&#39;);
<a name="line85"></a>  mockFoo.$replay();
<a name="line86"></a>  mockFoo(&#39;x&#39;);
<a name="line87"></a>  mockFoo.$verify();
<a name="line88"></a>}
<a name="line89"></a>
<a name="line90"></a>function testMocksFunctionWithMultipleArgs() {
<a name="line91"></a>  var mockFoo = goog.testing.createFunctionMock();
<a name="line92"></a>  mockFoo(&#39;x&#39;, &#39;y&#39;);
<a name="line93"></a>  mockFoo.$replay();
<a name="line94"></a>  mockFoo(&#39;x&#39;, &#39;y&#39;);
<a name="line95"></a>  mockFoo.$verify();
<a name="line96"></a>}
<a name="line97"></a>
<a name="line98"></a>function testFailsIfCalledWithIncorrectArgs() {
<a name="line99"></a>  var mockFoo = goog.testing.createFunctionMock();
<a name="line100"></a>
<a name="line101"></a>  mockFoo();
<a name="line102"></a>  mockFoo.$replay();
<a name="line103"></a>  assertThrows(function() {mockFoo(&#39;x&#39;);});
<a name="line104"></a>  mockFoo.$reset();
<a name="line105"></a>
<a name="line106"></a>  mockFoo(&#39;x&#39;);
<a name="line107"></a>  mockFoo.$replay();
<a name="line108"></a>  assertThrows(function() {mockFoo();});
<a name="line109"></a>  mockFoo.$reset();
<a name="line110"></a>
<a name="line111"></a>  mockFoo(&#39;x&#39;);
<a name="line112"></a>  mockFoo.$replay();
<a name="line113"></a>  assertThrows(function() {mockFoo(&#39;x&#39;, &#39;y&#39;);});
<a name="line114"></a>  mockFoo.$reset();
<a name="line115"></a>
<a name="line116"></a>  mockFoo(&#39;x&#39;, &#39;y&#39;);
<a name="line117"></a>  mockFoo.$replay();
<a name="line118"></a>  assertThrows(function() {mockFoo(&#39;x&#39;);});
<a name="line119"></a>  mockFoo.$reset();
<a name="line120"></a>
<a name="line121"></a>  mockFoo(&#39;correct&#39;);
<a name="line122"></a>  mockFoo.$replay();
<a name="line123"></a>  assertThrows(function() {mockFoo(&#39;wrong&#39;);});
<a name="line124"></a>  mockFoo.$reset();
<a name="line125"></a>
<a name="line126"></a>  mockFoo(&#39;correct&#39;, &#39;args&#39;);
<a name="line127"></a>  mockFoo.$replay();
<a name="line128"></a>  assertThrows(function() {mockFoo(&#39;wrong&#39;, &#39;args&#39;);});
<a name="line129"></a>  mockFoo.$reset();
<a name="line130"></a>}
<a name="line131"></a>
<a name="line132"></a>function testMocksFunctionWithReturnValue() {
<a name="line133"></a>  var mockFoo = goog.testing.createFunctionMock();
<a name="line134"></a>  mockFoo().$returns(&#39;bar&#39;);
<a name="line135"></a>  mockFoo.$replay();
<a name="line136"></a>  assertEquals(&#39;bar&#39;, mockFoo());
<a name="line137"></a>  mockFoo.$verify();
<a name="line138"></a>}
<a name="line139"></a>
<a name="line140"></a>function testFunctionMockWorksWhenPassedAsACallback() {
<a name="line141"></a>  var invoker = {
<a name="line142"></a>    register: function(callback) {
<a name="line143"></a>      this.callback = callback;
<a name="line144"></a>    },
<a name="line145"></a>
<a name="line146"></a>    invoke: function(args) {
<a name="line147"></a>      return this.callback(args);
<a name="line148"></a>    }
<a name="line149"></a>  };
<a name="line150"></a>
<a name="line151"></a>  var mockFunction = goog.testing.createFunctionMock();
<a name="line152"></a>  mockFunction(&#39;bar&#39;).$returns(&#39;baz&#39;);
<a name="line153"></a>
<a name="line154"></a>  mockFunction.$replay();
<a name="line155"></a>  invoker.register(mockFunction);
<a name="line156"></a>  assertEquals(&#39;baz&#39;, invoker.invoke(&#39;bar&#39;));
<a name="line157"></a>  mockFunction.$verify();
<a name="line158"></a>}
<a name="line159"></a>
<a name="line160"></a>function testFunctionMockQuacksLikeAStrictMock() {
<a name="line161"></a>  var mockFunction = goog.testing.createFunctionMock();
<a name="line162"></a>  assertQuacksLike(mockFunction, goog.testing.StrictMock);
<a name="line163"></a>}
<a name="line164"></a>
<a name="line165"></a>
<a name="line166"></a>//----- Global functions for goog.testing.GlobalFunctionMock to mock
<a name="line167"></a>
<a name="line168"></a>function globalFoo() {
<a name="line169"></a>  return &#39;I am Spartacus!&#39;;
<a name="line170"></a>}
<a name="line171"></a>
<a name="line172"></a>function globalBar(who, what) {
<a name="line173"></a>  return [who, &#39;is&#39;, what].join(&#39; &#39;);
<a name="line174"></a>}
<a name="line175"></a>
<a name="line176"></a>
<a name="line177"></a>//----- Tests for goog.testing.createGlobalFunctionMock
<a name="line178"></a>
<a name="line179"></a>function testMocksGlobalFunctionWithNoArgs() {
<a name="line180"></a>  mockGlobal = goog.testing.createGlobalFunctionMock(&#39;globalFoo&#39;);
<a name="line181"></a>  mockGlobal().$returns(&#39;No, I am Spartacus!&#39;);
<a name="line182"></a>
<a name="line183"></a>  mockGlobal.$replay();
<a name="line184"></a>  assertEquals(&#39;No, I am Spartacus!&#39;, globalFoo());
<a name="line185"></a>  mockGlobal.$verify();
<a name="line186"></a>}
<a name="line187"></a>
<a name="line188"></a>function testMocksGlobalFunctionUsingGlobalName() {
<a name="line189"></a>  goog.testing.createGlobalFunctionMock(&#39;globalFoo&#39;);
<a name="line190"></a>  globalFoo().$returns(&#39;No, I am Spartacus!&#39;);
<a name="line191"></a>
<a name="line192"></a>  globalFoo.$replay();
<a name="line193"></a>  assertEquals(&#39;No, I am Spartacus!&#39;, globalFoo());
<a name="line194"></a>  globalFoo.$verify();
<a name="line195"></a>  globalFoo.$tearDown();
<a name="line196"></a>}
<a name="line197"></a>
<a name="line198"></a>function testMocksGlobalFunctionWithArgs() {
<a name="line199"></a>  var mockReturnValue = &#39;Noam is Chomsky!&#39;;
<a name="line200"></a>  mockGlobal = goog.testing.createGlobalFunctionMock(&#39;globalBar&#39;);
<a name="line201"></a>  mockGlobal(&#39;Noam&#39;, &#39;Spartacus&#39;).$returns(mockReturnValue);
<a name="line202"></a>
<a name="line203"></a>  mockGlobal.$replay();
<a name="line204"></a>  assertEquals(mockReturnValue, globalBar(&#39;Noam&#39;, &#39;Spartacus&#39;));
<a name="line205"></a>  mockGlobal.$verify();
<a name="line206"></a>}
<a name="line207"></a>
<a name="line208"></a>function testGlobalFunctionMockFailsWithIncorrectArgs() {
<a name="line209"></a>  mockGlobal = goog.testing.createGlobalFunctionMock(&#39;globalBar&#39;);
<a name="line210"></a>  mockGlobal(&#39;a&#39;, &#39;b&#39;);
<a name="line211"></a>
<a name="line212"></a>  mockGlobal.$replay();
<a name="line213"></a>
<a name="line214"></a>  assertThrows(&#39;Mock should have failed because of incorrect arguments&#39;,
<a name="line215"></a>      function() {globalBar(&#39;b&#39;, &#39;a&#39;)});
<a name="line216"></a>}
<a name="line217"></a>
<a name="line218"></a>function testGlobalFunctionMockQuacksLikeAFunctionMock() {
<a name="line219"></a>  mockGlobal = goog.testing.createGlobalFunctionMock(&#39;globalFoo&#39;);
<a name="line220"></a>  assertQuacksLike(mockGlobal, goog.testing.FunctionMock);
<a name="line221"></a>}
<a name="line222"></a>
<a name="line223"></a>function testMockedFunctionsAvailableInGlobalAndGoogGlobalAndWindowScope() {
<a name="line224"></a>  mockGlobal = goog.testing.createGlobalFunctionMock(&#39;globalFoo&#39;);
<a name="line225"></a>
<a name="line226"></a>  // we expect this call 3 times through global, goog.global and window scope
<a name="line227"></a>  mockGlobal().$times(3);
<a name="line228"></a>
<a name="line229"></a>  mockGlobal.$replay();
<a name="line230"></a>  goog.global.globalFoo();
<a name="line231"></a>  window.globalFoo();
<a name="line232"></a>  globalFoo();
<a name="line233"></a>  mockGlobal.$verify();
<a name="line234"></a>}
<a name="line235"></a>
<a name="line236"></a>function testTearDownRestoresOriginalGlobalFunction() {
<a name="line237"></a>  mockGlobal = goog.testing.createGlobalFunctionMock(&#39;globalFoo&#39;);
<a name="line238"></a>  mockGlobal().$returns(&#39;No, I am Spartacus!&#39;);
<a name="line239"></a>
<a name="line240"></a>  mockGlobal.$replay();
<a name="line241"></a>  assertEquals(&#39;No, I am Spartacus!&#39;, globalFoo());
<a name="line242"></a>  mockGlobal.$tearDown();
<a name="line243"></a>  assertEquals(&#39;I am Spartacus!&#39;, globalFoo());
<a name="line244"></a>  mockGlobal.$verify();
<a name="line245"></a>}
<a name="line246"></a>
<a name="line247"></a>function testTearDownHandlesMultipleMocking() {
<a name="line248"></a>  var mock1 = goog.testing.createGlobalFunctionMock(&#39;globalFoo&#39;);
<a name="line249"></a>  var mock2 = goog.testing.createGlobalFunctionMock(&#39;globalFoo&#39;);
<a name="line250"></a>  var mock3 = goog.testing.createGlobalFunctionMock(&#39;globalFoo&#39;);
<a name="line251"></a>  mock1().$returns(&#39;No, I am Spartacus 1!&#39;);
<a name="line252"></a>  mock2().$returns(&#39;No, I am Spartacus 2!&#39;);
<a name="line253"></a>  mock3().$returns(&#39;No, I am Spartacus 3!&#39;);
<a name="line254"></a>
<a name="line255"></a>  mock1.$replay();
<a name="line256"></a>  mock2.$replay();
<a name="line257"></a>  mock3.$replay();
<a name="line258"></a>  assertEquals(&#39;No, I am Spartacus 3!&#39;, globalFoo());
<a name="line259"></a>  mock3.$tearDown();
<a name="line260"></a>  assertEquals(&#39;No, I am Spartacus 2!&#39;, globalFoo());
<a name="line261"></a>  mock2.$tearDown();
<a name="line262"></a>  assertEquals(&#39;No, I am Spartacus 1!&#39;, globalFoo());
<a name="line263"></a>  mock1.$tearDown();
<a name="line264"></a>  assertEquals(&#39;I am Spartacus!&#39;, globalFoo());
<a name="line265"></a>}
<a name="line266"></a>
<a name="line267"></a>function testGlobalFunctionMockCallOrdering() {
<a name="line268"></a>  var mock = goog.testing.createGlobalFunctionMock(&#39;globalFoo&#39;);
<a name="line269"></a>  mock(1);
<a name="line270"></a>  mock(2);
<a name="line271"></a>  mock.$replay();
<a name="line272"></a>  assertThrows(function() {globalFoo(2);});
<a name="line273"></a>  mock.$tearDown();
<a name="line274"></a>
<a name="line275"></a>  mock = goog.testing.createGlobalFunctionMock(&#39;globalFoo&#39;,
<a name="line276"></a>                                               goog.testing.Mock.STRICT);
<a name="line277"></a>  mock(1);
<a name="line278"></a>  mock(2);
<a name="line279"></a>  mock.$replay();
<a name="line280"></a>  globalFoo(1);
<a name="line281"></a>  globalFoo(2);
<a name="line282"></a>  mock.$verify();
<a name="line283"></a>  mock.$tearDown();
<a name="line284"></a>
<a name="line285"></a>  mock = goog.testing.createGlobalFunctionMock(&#39;globalFoo&#39;,
<a name="line286"></a>                                               goog.testing.Mock.STRICT);
<a name="line287"></a>  mock(1);
<a name="line288"></a>  mock(2);
<a name="line289"></a>  mock.$replay();
<a name="line290"></a>  assertThrows(function() {globalFoo(2);});
<a name="line291"></a>  mock.$tearDown();
<a name="line292"></a>
<a name="line293"></a>  mock = goog.testing.createGlobalFunctionMock(&#39;globalFoo&#39;,
<a name="line294"></a>                                               goog.testing.Mock.LOOSE);
<a name="line295"></a>  mock(1);
<a name="line296"></a>  mock(2);
<a name="line297"></a>  mock.$replay();
<a name="line298"></a>  globalFoo(2);
<a name="line299"></a>  globalFoo(1);
<a name="line300"></a>  mock.$verify();
<a name="line301"></a>  mock.$tearDown();
<a name="line302"></a>}
<a name="line303"></a>
<a name="line304"></a>//----- Functions for goog.testing.MethodMock to mock
<a name="line305"></a>
<a name="line306"></a>var mynamespace = {};
<a name="line307"></a>
<a name="line308"></a>mynamespace.myMethod = function() {
<a name="line309"></a>  return &#39;I should be mocked.&#39;;
<a name="line310"></a>};
<a name="line311"></a>
<a name="line312"></a>function testMocksMethod() {
<a name="line313"></a>  mockMethod = goog.testing.createMethodMock(mynamespace, &#39;myMethod&#39;);
<a name="line314"></a>  mockMethod().$returns(&#39;I have been mocked!&#39;);
<a name="line315"></a>
<a name="line316"></a>  mockMethod.$replay();
<a name="line317"></a>  assertEquals(&#39;I have been mocked!&#39;, mockMethod());
<a name="line318"></a>  mockMethod.$verify();
<a name="line319"></a>}
<a name="line320"></a>
<a name="line321"></a>function testMocksMethodInNamespace() {
<a name="line322"></a>  goog.testing.createMethodMock(mynamespace, &#39;myMethod&#39;);
<a name="line323"></a>  mynamespace.myMethod().$returns(&#39;I have been mocked!&#39;);
<a name="line324"></a>
<a name="line325"></a>  mynamespace.myMethod.$replay();
<a name="line326"></a>  assertEquals(&#39;I have been mocked!&#39;, mynamespace.myMethod());
<a name="line327"></a>  mynamespace.myMethod.$verify();
<a name="line328"></a>  mynamespace.myMethod.$tearDown();
<a name="line329"></a>}
<a name="line330"></a>
<a name="line331"></a>function testMethodMockCanOnlyMockExistingMethods() {
<a name="line332"></a>  assertThrows(function() {
<a name="line333"></a>    goog.testing.createMethodMock(mynamespace, &#39;doesNotExist&#39;);
<a name="line334"></a>  });
<a name="line335"></a>}
<a name="line336"></a>
<a name="line337"></a>function testMethodMockCallOrdering() {
<a name="line338"></a>  goog.testing.createMethodMock(mynamespace, &#39;myMethod&#39;);
<a name="line339"></a>  mynamespace.myMethod(1);
<a name="line340"></a>  mynamespace.myMethod(2);
<a name="line341"></a>  mynamespace.myMethod.$replay();
<a name="line342"></a>  assertThrows(function() {mynamespace.myMethod(2);});
<a name="line343"></a>  mynamespace.myMethod.$tearDown();
<a name="line344"></a>
<a name="line345"></a>  goog.testing.createMethodMock(mynamespace, &#39;myMethod&#39;,
<a name="line346"></a>                                goog.testing.Mock.STRICT);
<a name="line347"></a>  mynamespace.myMethod(1);
<a name="line348"></a>  mynamespace.myMethod(2);
<a name="line349"></a>  mynamespace.myMethod.$replay();
<a name="line350"></a>  mynamespace.myMethod(1);
<a name="line351"></a>  mynamespace.myMethod(2);
<a name="line352"></a>  mynamespace.myMethod.$verify();
<a name="line353"></a>  mynamespace.myMethod.$tearDown();
<a name="line354"></a>
<a name="line355"></a>  goog.testing.createMethodMock(mynamespace, &#39;myMethod&#39;,
<a name="line356"></a>                                goog.testing.Mock.STRICT);
<a name="line357"></a>  mynamespace.myMethod(1);
<a name="line358"></a>  mynamespace.myMethod(2);
<a name="line359"></a>  mynamespace.myMethod.$replay();
<a name="line360"></a>  assertThrows(function() {mynamespace.myMethod(2);});
<a name="line361"></a>  mynamespace.myMethod.$tearDown();
<a name="line362"></a>
<a name="line363"></a>  goog.testing.createMethodMock(mynamespace, &#39;myMethod&#39;,
<a name="line364"></a>                                goog.testing.Mock.LOOSE);
<a name="line365"></a>  mynamespace.myMethod(1);
<a name="line366"></a>  mynamespace.myMethod(2);
<a name="line367"></a>  mynamespace.myMethod.$replay();
<a name="line368"></a>  mynamespace.myMethod(2);
<a name="line369"></a>  mynamespace.myMethod(1);
<a name="line370"></a>  mynamespace.myMethod.$verify();
<a name="line371"></a>  mynamespace.myMethod.$tearDown();
<a name="line372"></a>}
<a name="line373"></a>
<a name="line374"></a>//----- Functions for goog.testing.createConstructorMock to mock
<a name="line375"></a>
<a name="line376"></a>var constructornamespace = {};
<a name="line377"></a>
<a name="line378"></a>constructornamespace.MyConstructor = function() {
<a name="line379"></a>};
<a name="line380"></a>
<a name="line381"></a>constructornamespace.MyConstructor.prototype.myMethod = function() {
<a name="line382"></a>  return &#39;I should be mocked.&#39;;
<a name="line383"></a>};
<a name="line384"></a>
<a name="line385"></a>constructornamespace.MyConstructorWithArgument = function(argument) {
<a name="line386"></a>  this.argument_ = argument;
<a name="line387"></a>};
<a name="line388"></a>
<a name="line389"></a>constructornamespace.MyConstructorWithArgument.prototype.myMethod = function() {
<a name="line390"></a>  return this.argument_;
<a name="line391"></a>};
<a name="line392"></a>
<a name="line393"></a>constructornamespace.MyConstructorWithClassMembers = function() {
<a name="line394"></a>};
<a name="line395"></a>
<a name="line396"></a>constructornamespace.MyConstructorWithClassMembers.CONSTANT = 42;
<a name="line397"></a>
<a name="line398"></a>constructornamespace.MyConstructorWithClassMembers.classMethod = function() {
<a name="line399"></a>  return &#39;class method return value&#39;;
<a name="line400"></a>};
<a name="line401"></a>
<a name="line402"></a>function testConstructorMock() {
<a name="line403"></a>  var mockObject =
<a name="line404"></a>      new goog.testing.StrictMock(constructornamespace.MyConstructor);
<a name="line405"></a>  var mockConstructor = goog.testing.createConstructorMock(
<a name="line406"></a>      constructornamespace, &#39;MyConstructor&#39;);
<a name="line407"></a>  mockConstructor().$returns(mockObject);
<a name="line408"></a>  mockObject.myMethod().$returns(&#39;I have been mocked!&#39;);
<a name="line409"></a>
<a name="line410"></a>  mockConstructor.$replay();
<a name="line411"></a>  mockObject.$replay();
<a name="line412"></a>  assertEquals(&#39;I have been mocked!&#39;,
<a name="line413"></a>      new constructornamespace.MyConstructor().myMethod());
<a name="line414"></a>  mockConstructor.$verify();
<a name="line415"></a>  mockObject.$verify();
<a name="line416"></a>  mockConstructor.$tearDown();
<a name="line417"></a>}
<a name="line418"></a>
<a name="line419"></a>function testConstructorMockWithArgument() {
<a name="line420"></a>  var mockObject = new goog.testing.StrictMock(
<a name="line421"></a>      constructornamespace.MyConstructorWithArgument);
<a name="line422"></a>  var mockConstructor = goog.testing.createConstructorMock(
<a name="line423"></a>      constructornamespace, &#39;MyConstructorWithArgument&#39;);
<a name="line424"></a>  mockConstructor(goog.testing.mockmatchers.isString).$returns(mockObject);
<a name="line425"></a>  mockObject.myMethod().$returns(&#39;I have been mocked!&#39;);
<a name="line426"></a>
<a name="line427"></a>  mockConstructor.$replay();
<a name="line428"></a>  mockObject.$replay();
<a name="line429"></a>  assertEquals(&#39;I have been mocked!&#39;,
<a name="line430"></a>      new constructornamespace.MyConstructorWithArgument(&#39;I should be mocked.&#39;)
<a name="line431"></a>          .myMethod());
<a name="line432"></a>  mockConstructor.$verify();
<a name="line433"></a>  mockObject.$verify();
<a name="line434"></a>  mockConstructor.$tearDown();
<a name="line435"></a>}
<a name="line436"></a>
<a name="line437"></a>
<a name="line438"></a>/**
<a name="line439"></a> * Test that class members are copied to the mock constructor.
<a name="line440"></a> */
<a name="line441"></a>function testConstructorMockWithClassMembers() {
<a name="line442"></a>  var mockConstructor = goog.testing.createConstructorMock(
<a name="line443"></a>      constructornamespace, &#39;MyConstructorWithClassMembers&#39;);
<a name="line444"></a>  assertEquals(42, constructornamespace.MyConstructorWithClassMembers.CONSTANT);
<a name="line445"></a>  assertEquals(&#39;class method return value&#39;,
<a name="line446"></a>      constructornamespace.MyConstructorWithClassMembers.classMethod());
<a name="line447"></a>  mockConstructor.$tearDown();
<a name="line448"></a>}
<a name="line449"></a>
<a name="line450"></a>function testConstructorMockCallOrdering() {
<a name="line451"></a>  var instance = {};
<a name="line452"></a>
<a name="line453"></a>  goog.testing.createConstructorMock(constructornamespace,
<a name="line454"></a>                                     &#39;MyConstructorWithArgument&#39;);
<a name="line455"></a>  constructornamespace.MyConstructorWithArgument(1).$returns(instance);
<a name="line456"></a>  constructornamespace.MyConstructorWithArgument(2).$returns(instance);
<a name="line457"></a>  constructornamespace.MyConstructorWithArgument.$replay();
<a name="line458"></a>  assertThrows(
<a name="line459"></a>      function() {new constructornamespace.MyConstructorWithArgument(2);});
<a name="line460"></a>  constructornamespace.MyConstructorWithArgument.$tearDown();
<a name="line461"></a>
<a name="line462"></a>  goog.testing.createConstructorMock(constructornamespace,
<a name="line463"></a>                                     &#39;MyConstructorWithArgument&#39;,
<a name="line464"></a>                                     goog.testing.Mock.STRICT);
<a name="line465"></a>  constructornamespace.MyConstructorWithArgument(1).$returns(instance);
<a name="line466"></a>  constructornamespace.MyConstructorWithArgument(2).$returns(instance);
<a name="line467"></a>  constructornamespace.MyConstructorWithArgument.$replay();
<a name="line468"></a>  new constructornamespace.MyConstructorWithArgument(1);
<a name="line469"></a>  new constructornamespace.MyConstructorWithArgument(2);
<a name="line470"></a>  constructornamespace.MyConstructorWithArgument.$verify();
<a name="line471"></a>  constructornamespace.MyConstructorWithArgument.$tearDown();
<a name="line472"></a>
<a name="line473"></a>  goog.testing.createConstructorMock(constructornamespace,
<a name="line474"></a>                                     &#39;MyConstructorWithArgument&#39;,
<a name="line475"></a>                                     goog.testing.Mock.STRICT);
<a name="line476"></a>  constructornamespace.MyConstructorWithArgument(1).$returns(instance);
<a name="line477"></a>  constructornamespace.MyConstructorWithArgument(2).$returns(instance);
<a name="line478"></a>  constructornamespace.MyConstructorWithArgument.$replay();
<a name="line479"></a>  assertThrows(
<a name="line480"></a>      function() {new constructornamespace.MyConstructorWithArgument(2);});
<a name="line481"></a>  constructornamespace.MyConstructorWithArgument.$tearDown();
<a name="line482"></a>
<a name="line483"></a>  goog.testing.createConstructorMock(constructornamespace,
<a name="line484"></a>                                     &#39;MyConstructorWithArgument&#39;,
<a name="line485"></a>                                     goog.testing.Mock.LOOSE);
<a name="line486"></a>  constructornamespace.MyConstructorWithArgument(1).$returns(instance);
<a name="line487"></a>  constructornamespace.MyConstructorWithArgument(2).$returns(instance);
<a name="line488"></a>  constructornamespace.MyConstructorWithArgument.$replay();
<a name="line489"></a>  new constructornamespace.MyConstructorWithArgument(2);
<a name="line490"></a>  new constructornamespace.MyConstructorWithArgument(1);
<a name="line491"></a>  constructornamespace.MyConstructorWithArgument.$verify();
<a name="line492"></a>  constructornamespace.MyConstructorWithArgument.$tearDown();
<a name="line493"></a>}
<a name="line494"></a>
<a name="line495"></a>//----- Helper assertions
<a name="line496"></a>
<a name="line497"></a>function assertQuacksLike(obj, target) {
<a name="line498"></a>  for (meth in target.prototype) {
<a name="line499"></a>    if (!goog.string.endsWith(meth, &#39;_&#39;)) {
<a name="line500"></a>      assertNotUndefined(&#39;Should have implemented &#39; + meth + &#39;()&#39;, obj[meth]);
<a name="line501"></a>    }
<a name="line502"></a>  }
<a name="line503"></a>}
</pre>


</body>
</html>
